package org.example.springboot.mapper;

import org.example.springboot.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("insert into `user` (username, password, name, phone, email, address, avatar) " +
            "VALUES (#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{avatar})")
    void insert(User user);

    @Update("update `user` set username = #{username}, password = #{password}," +
            "name = #{name}, phone = #{phone}, email = #{email}, address = #{address}, avatar = #{avatar} where id = #{id}")
    void update(User user);

    @Delete("delete from `user` where id = #{id}")
    void delete(Integer id);

    @Select("select * from `user` order by id desc ")
    List<User> selectAll();

    @Select("select * from `user` where id = #{id}")
    User selectById(Integer id);

    @Select("select * from `user` where username = #{username}")
    User selectByUsername(String username);

    @Select("select * from `user` where name = #{name} and phone = #{phone} order by id desc ")
    List<User> selectByMore(@Param("name") String name, @Param("phone") String phone);

    @Select("select * from `user` where name like concat('%',#{name},'%') and phone like concat('%',#{phone},'%') order by id desc ")
    List<User> selectByMo(@Param("name") String name, @Param("phone") String phone);

    @Select("select * from `user` where name like concat('%',#{name},'%') and phone like concat('%',#{phone},'%') order by id desc limit #{skipNum}, #{pageSize}")
    List<User> selectByPage(@Param("name") String name, @Param("phone") String phone, @Param("skipNum") Integer skipNum, @Param("pageSize") Integer pageSize);

    @Select("select count(id) from `user` where name like concat('%',#{name},'%') and phone like concat('%',#{phone},'%') order by id desc")
    Integer selectByCountPage(@Param("name") String name, @Param("phone") String phone);

}
